Skip to content

fix(node): close the gaps found in parallel task handling - #15

Merged
TudorAndrei merged 24 commits into
mainfrom
t3/a1294d36
Nov 13, 2026
Merged

TudorAndrei merged 24 commits into
mainfrom
t3/a1294d36

Conversation

@TudorAndrei

@TudorAndrei TudorAndrei commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Why

Parallel task execution exposed gaps that could let a recovered task start twice, leave an agent disconnected after a malformed capacity value, or make each Codex turn retry a stale binary path. This change closes those gaps and documents the brief capacity overrun possible during an agent reconnect.

What changes

flowchart LR
    A[Recovered task queued] --> B{Original run still active?}
    B -->|Yes| C[Keep task queued]
    B -->|No| D[Lease task]
    E[Agent sends ready] --> F{max_tasks is a valid integer?}
    F -->|No| G[Use capacity 1]
    F -->|Yes| H[Use requested capacity]
    I[Codex binary moved] --> J[Resolve and share updated path]
    J --> K[Later turns use updated path]
    classDef changed fill:#e7f5e8,stroke:#31834a
    class C,G,K changed
Loading
  • The running-task exclusion keeps recovery from leasing a task while its previous run is still alive.
  • Invalid max_tasks values fall back to one, so a malformed ready message does not prevent attachment.
  • The Codex binary path is shared across turns after it is resolved again. The agent-socket README explains that capacity is checked at reservation time, so reconnecting with a lower limit can briefly leave more tasks in flight than requested.

Look at: crates/aether/src/agent/connected.rs — running-task exclusion and capacity parsing.

Evidence

  • cargo fmt --all -- --check — passed.
  • cargo clippy --workspace --all-targets -- -D warnings — passed.
  • cargo test --workspace — passed: 561 unit tests and 98 feature scenarios (553 steps).
  • Hub and plugin suites were not rerun for this change; the hub is untouched, and the agent-socket contract and transcript are unchanged.
  • The final pass of the model check with restarts did not complete before time ran out, so cancellation liveness with restarts and a limit of 2 remains unchecked.

Merge danger

Door: two-way
Blast radius: one node

The change affects node scheduling and agent-socket capacity handling. It does not change the wire contract, invite format, Agent Card, or hub protocol, so nodes remain compatible with released nodes and the hub. Owners do not need a restart for a policy or settings change.

After merge


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

TudorAndrei and others added 24 commits November 13, 2026 15:54
Wait 20 ms before the first re-poll and double up to the 250 ms cap
instead of a fixed 250 ms. The wire protocol is unchanged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
reqwest otherwise builds on aws-lc-rs, whose first RAND_bytes call (the
ClientHello random) seeds jitter entropy for ~100 ms of CPU.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…unks

- Add AetherHub.ReadRepo: 4 read-only, deferred connections on the same WAL
  file, started after the migrations. Repo stays the single immediate writer.
- Invites.snapshot and Versions.current are pure reads and now run on the
  read pool (snapshot in one consistent deferred transaction). Long polls
  still read after the writer's commit, so they see it.
- Invites.delete_expired deletes in transactions of at most 500 rows and
  bumps that chunk's endpoints in the same transaction. Versions.bump
  deduplicates and updates in chunks of 500. A backlog above SQLite's
  32,766-variable limit no longer crashes the sweeper, and the writer is
  held for one chunk at a time.
- Document the two pools in the server README; add tests for the read
  pool and for sweeps above the variable limit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…uest accounts

- GET /directory.json is built once in 15 s by AetherHub.DirectoryCache (ETS
  read path, one rebuild at a time, stale value on a failed build). The build
  reads light columns for every live listing, decodes the 100 on the page,
  fetches their owners in one query, and uses a new accounts(kind) index for
  the people count: 5 queries instead of 102.
- Listings.search and the directory read owners with OwnerLinks.active_owners
  (one query) instead of one query per result. Results and order are unchanged.
- Signed requests read their account from AccountCache. Every account write
  in Accounts (claim, change, rename, delete) invalidates it inside its
  transaction and after it; a reader drops a copy that a write overtook.
- Migration 20261113000000 adds accounts_kind_index (IF NOT EXISTS).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The store passed string SQL to execute and query_row, so SQLite parsed it
on every call (about 8% of node instructions). Each statement now goes
through prepare_cached, and a connection keeps 256 statements instead of
16, so the task, lease, event, outbox and configuration statements stay
prepared.

Also write six empty-list test assertions as assert_eq, which the 1.99
clippy asks for.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
- settings::setting reads on a per-directory store that stays open (8
  directories at most). It is dropped when the database file is deleted
  or replaced, or when a read fails on it. Each read starts a new read
  transaction, so a change from another process or the admin UI is seen.
- The outbox loop keeps one store across rounds instead of opening it
  every second; enqueue opens it once instead of twice. After failed
  rounds the loop pauses 1 s, 2 s, 4 s, up to 30 s, and a good round
  resets the pause.
- RosterClaims keeps its roster store open between admissions.
- PolicyEngine admission verified every matching grant's ed25519
  signature each time. The signature result is now remembered for the
  exact key, signature and signed bytes (1024 entries). Trust, expiry,
  scope and every other check still run on each admission, and a failed
  signature is not remembered.

The TLA+ models are unchanged: no modeled step changes its result.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The faster task polling made back-to-back `node request` calls from one
peer exceed the host's 8-per-second limit, and every call after the
fourth failed with "node overloaded".

The host now adds `data.retryAfterMs` to the overloaded error that its
rate limit sends; the message and code stay the same, so older callers
see no change. The client sends such an exchange again, with the same
idempotency key, for up to 2 s, starting at one rate-limit token and
doubling. A full queue sends no `data` and is still rejected at once, as
the "Replay and overload protection" feature requires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With `accounts_kind_index`, SQLite answered `GET /v1/people?q=` by
reading every personal account through the kind index and sorting them,
instead of the name range on `accounts_name_number_index`. At 64 clients
people search fell from 2,309 to 485 req/s. The index only sped up the
directory's count of people, which the directory cache now runs at most
once each 15 s. The migration was never released, so it is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A write to accounts from outside the VM stayed invisible until restart; a
copy now lasts 60 s (AetherHub.Clock time). A reader also claims the row
with insert_new and turns its own mark into the copy with an atomic
select_replace, so invalidate/1 deleting the row leaves nothing to store
and the insert/delete_object window is gone.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… is down

After a failed build the cache serves the old value for 2 s without a new
build, and fails fast with no old value, so callers no longer queue a
build each. A read while the cache process restarts raises Unavailable,
which the route answers as 503. The route sends an age header so the 15 s
max-age is honest. PROTOCOL.md says that listed and online count listings
that fail to decode, and describes age and the 503.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The outbox sent through `Link::exchange` and mapped every "node overloaded"
to a full queue, so the per-peer rate limit parked a running task for 5 s
and up to 300 s with a wrong reason. `follow` now sends a shed frame again,
within the overload window and the attempt window, and `outcome` gives a
rate-limited answer its own reason and a fixed 2 s delay. A full queue
keeps the `Busy` backoff and is not sent again.

No wait of the client or the outbox passes the end of the overload window
or of its own limit (`overload_retry_wait`), so the real window is the
stated 2 s, not up to 2.9 s.

The outbox loop kept its store open and never noticed a replaced database
file, so after a restored backup `enqueue` wrote to the new file while the
loop read the old one. It now uses `KeptStore`. `KeptStore` also checks
`PRAGMA user_version` before each use and opens through `NodeStore::open`
when it differs, so a file that a newer binary migrated fails closed again.

Tests: the retry loop against a host with a per-peer quota of 1 (retry
succeeds, a stuck peer gets the error at the window end, a full queue
returns at once), the shed answer through `Link::exchange`, the outbox
retry and reason, and a replaced file and a newer schema for the outbox and
`KeptStore`. The TLA+ models EventPaging, ManagedRoster and Outbox get
notes that explain why the change is a stutter step; TLC finds no error.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The key is sent only by the per-peer rate limit, its value is a fixed
one-token hint, a full queue never carries it, and a client may send the
same exchange again only when it is present.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e contract

`/directory.json` answers `503 unavailable` when it has no view to serve,
and sends `max-age` with an `age` header. Both were only in PROTOCOL.md.
The contract now has the error code `unavailable` (503) and the route's
`cacheSeconds` (15). The hub takes its max-age and the cache time to live
from one constant that a test checks against the contract, answers 503
through the usual error reply, and the node maps `unavailable` to
`HubError::Unreachable`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `ready` message can carry `max_tasks`. Without it an agent takes one
task at a time, as before. The Hermes and OpenClaw plugins say 32: each
task is its own session with its own stop, so they run side by side. The
Grok Bot bridge says 1: a Bot claims one task at a time, and a second task
would wait in the mailbox until its time ran out.

The transcript gets a case with two open tasks (`concurrent`), which the
bridge skips. Each plugin's tests replay it and add a test of two open
tasks.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…owner sets

The new limit `running_tasks` (`QueueLimitId::Running`, policy field
`runningTasks`) says how many tasks the node runs at once, for all callers
together. It is 1 for a new node and for a policy that lacks it, so a node
behaves as before until its owner raises it. It is 1 to 32. It is stored,
validated and audited like the queue limits: the Advanced page, the admin
socket and `aether.settings.update_policy` (`running_tasks`) set it for the
next start, and `aether.change` sets it on a running node. A higher value
waits for the owner's confirmation; a lower one applies at once and never
stops a task that runs.

The scheduler keeps a set of runs (`RunningTasks`) instead of one
`executing` flag, and starts a run on its own thread, with its own lease
renewal, while fewer than the limit run. `Executor::prepare_turn` checks a
task under the executor lock and hands back a turn that runs without it, so
turns overlap; each start is still confirmed once under `dispatch_serial`.
Cancel, recovery of a lost lease and shutdown work per task.

A task whose lease ran out before its run could renew it went back to the
queue and could be leased again while its first run lived. The first run's
lost lease then cancelled the second run's turn (TLC found this in the
concurrent model). `lease_next_except` skips the tasks that a run of the
node still holds.

`Agent::capacity` lets an adapter run fewer tasks than the owner allows:
Codex with `workspace-write` (one shared folder) says 1, and a connected
agent says what its `ready` carried. Concurrent Codex turns copy the
binary into their task folders and start it; a copy that overlapped a fork
made another turn fail with ETXTBSY (3 of 100 tasks with four at once), so
a lock now covers the copy and the start. `NodeAgent` also forwards
`Agent::forget` to its adapters, which it had not done.

Specs: ExecutionLease has one worker per execution, one turn per task and a
`cap` that can change at any time (new ExecutionLeaseSerial keeps the
default limit with restarts); ConnectedAgent checks two open tasks and
ConnectedAgentRerun keeps the earlier one-task, two-turn run; PendingChange
has two limits.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Test that the scheduler gives a task no second run while its first run
  lives: a task that recovery put back in the queue stays queued until
  that run ends, and the agent sees one turn. Without the
  `tasks_in_flight` exclusion the task is leased again and the test
  fails; before, only the store test and the TLA+ model covered it.
- A `ready` whose `max_tasks` is not a whole number (2.5, -1, "2", null)
  now attaches the agent with one task at a time. The whole line used to
  fail to parse, so the node skipped it and the agent was never attached,
  with no error on either side.
- The agent socket README states that the node checks `max_tasks` when it
  reserves a slot, not when it sends the task, so an agent that replaces
  one with a larger `max_tasks` while tasks start can briefly hold more.
  Refusing the task in `run_turn` would fail it, because its start is
  already confirmed then.
- `CodexAppServer` keeps the binary path that it found again after an
  update in state that every turn's clone shares, so later turns do not
  each try the stale path first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TudorAndrei
TudorAndrei merged commit a5ed014 into main Nov 13, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant